﻿using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;

namespace Conducido.BizTalkOppSvc.Services.Implementation
{
    //TODO: Dit moet echt anders.
    /// <summary>
    /// Loads resources
    /// </summary>
    public static class ResourceStringLoader
    {
        //TODO: Nog toevoegen
        public static string GetErrorDescription(int errorCode, string baseName, Assembly assembly)
        {

            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (string.IsNullOrEmpty(baseName))
            {
                throw new ArgumentNullException("baseName");
            }
            var manager = CreateResourceManager(baseName, assembly);
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            return manager.GetString("e" + Convert.ToString(errorCode, NumberFormatInfo.CurrentInfo), currentCulture);
        }

        public static string GetMessage(string messageCode, string baseName, Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (string.IsNullOrEmpty(messageCode))
            {
                throw new ArgumentNullException("messageCode");
            }
            if (string.IsNullOrEmpty(baseName))
            {
                throw new ArgumentNullException("baseName");
            }

            var manager = CreateResourceManager(baseName, assembly);
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            return manager.GetString(messageCode, currentCulture);
        }

        public static string GetMessage(string messageCode, string baseName, Assembly assembly,
                                        params object[] arguments)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            if (string.IsNullOrEmpty(messageCode))
            {
                throw new ArgumentNullException("messageCode");
            }
            if (string.IsNullOrEmpty(baseName))
            {
                throw new ArgumentNullException("baseName");
            }
            var manager = CreateResourceManager(baseName, assembly);
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            string format = manager.GetString(messageCode, currentCulture);
            return string.Format(CultureInfo.CurrentCulture, format, arguments);
        }

        private static ResourceManager CreateResourceManager(string baseName, Assembly assembly)
        {
            return new ResourceManager(baseName, assembly);
        }
    }
}